Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Array → Loop Arrays

Python Array

Loop Arrays

Looping Through Arrays (Lists) in Python: A Detailed Explanation

In Python, arrays are typically represented using lists. Lists are versatile, ordered, mutable (changeable) sequences that can hold elements of different data types. Looping through a list allows you to process each element individually. Let's explore various ways to achieve this, with detailed explanations and diverse examples.

1. Using `for` loop with index

This method iterates through the list using its index (position). The `len()` function provides the list's length, determining the loop's termination condition.
Looping array using for loop my_list = ["apple", "banana", "cherry", "date"] for i in range(len(my_list)): print(f"Element at index {i}: {my_list[i]}")

Output

Element at index 0: apple Element at index 1: banana Element at index 2: cherry Element at index 3: date

Explanation

- `range(len(my_list))` generates a sequence of numbers from 0 up to (but not including) the length of `my_list`. - The loop variable `i` takes each number in the sequence. - `my_list[i]` accesses the element at index `i`.

2. Using `for` loop directly

This is a more Pythonic and concise way. It directly iterates over the *elements* of the list, without needing to manage indices explicitly.
Looping using for loop my_list = ["apple", "banana", "cherry", "date"] for item in my_list: print(f"Item: {item}")

Output

Item: apple Item: banana Item: cherry Item: date

Explanation

- The loop variable `item` takes each element from `my_list` in sequence. This simplifies the code and improves readability.

3. Using `while` loop with index

This offers more control, allowing for complex iteration logic. You need to manually manage the index and loop termination condition.
Looping array using while loop my_list = ["apple", "banana", "cherry", "date"] i = 0 while i < len(my_list): print(f"Element at index {i}: {my_list[i]}") i += 1

Output

Element at index 0: apple Element at index 1: banana Element at index 2: cherry Element at index 3: date

Explanation

- The `while` loop continues as long as `i` is less than the list's length. - `i += 1` increments the index after each iteration.

4. Looping and Modifying elements

You can modify list elements within the loop.
Looping and modifying array my_list = [1, 2, 3, 4, 5] for i in range(len(my_list)): my_list[i] *= 2 # Double each element print(my_list) # Output: [2, 4, 6, 8, 10]

Output

[2, 4, 6, 8, 10]

5. Looping through Multidimensional Lists (Lists of Lists)

Nested loops are used for multidimensional lists.
Looping through Multidimensional Array matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] for row in matrix: for element in row: print(element, end=" ") # Print elements on the same line print() # Move to the next line after each row

Output

1 2 3 4 5 6 7 8 9

Explanation

- The outer loop iterates through each row (which is itself a list). - The inner loop iterates through each element within the current row.

6. List Comprehensions (for concise looping and creation)

List comprehensions provide a compact way to create new lists based on existing ones.
List Comprehensions numbers = [1, 2, 3, 4, 5] squared_numbers = [x for x in numbers] print(squared_numbers) # Output: [1, 4, 9, 16, 25] even_numbers = [x for x in numbers if x % 2 == 0] print(even_numbers) # Output: [2, 4]

Output

[1, 2, 3, 4, 5] [2, 4]

These examples demonstrate various methods for looping through lists in Python, catering to different needs and coding styles. Choosing the appropriate method depends on the specific task and desired level of control. Remember to select the most readable and efficient approach for your situation.

Tutorials